﻿using Kingdee.BOS;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.Contracts.Report;
using Kingdee.BOS.Core.Report;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Kingdee.BOS.Core.Enums.BOSEnums;

namespace RK.ReportPlugin.BPW
{
    [Description("BPW_简单账表：BPW即时库存明细"), HotUpdate]
    public class DzInventory : SysReportBaseService
    {
        public override void Initialize()
        {
            base.Initialize();
            this.ReportProperty.ReportType = ReportType.REPORTTYPE_NORMAL;
            this.ReportProperty.ReportName = new LocaleValue("BPW即时库存明细", base.Context.UserLocale.LCID);
            this.IsCreateTempTableByPlugin = true;
            this.ReportProperty.IsUIDesignerColumns = false;
            this.ReportProperty.IsGroupSummary = true;
            this.ReportProperty.SimpleAllCols = false;
            this.ReportProperty.PrimaryKeyFieldName = "FMATERIALID";
            this.ReportProperty.IdentityFieldName = "seq";
        }
        protected override string BuilderTempTableOrderBySQL(IRptParams filter)
        {
            string sortStr = filter.FilterParameter.SortString;
            if (sortStr.Length > 0)
            {
                return sortStr;
            }
            return " FMATERIALID ";
        }
        public override void BuilderReportSqlAndTempTable(IRptParams filter, string tableName)
        {
            string seqFld = string.Format(base.KSQL_SEQ, BuilderTempTableOrderBySQL(filter));
            string createSql = @"/*dialect*/ CREATE TABLE #TEMP (FIDENTITYID INT NULL,FMATERIALID VARCHAR(255) NULL,FmatName VARCHAR(255) NULL,
            FSPECIFICATION VARCHAR(255) NULL,
            FBOMID VARCHAR(255) NULL,FSTOCKID VARCHAR(255) NULL,FLOT VARCHAR(50) NULL,FUNITID VARCHAR(50) NULL,FBASEQTY DECIMAL(38,10) NOT NULL,
            F_BPW_DWGNUMBER VARCHAR(50) NULL
            ) ";
            string filterStr = "";
            if (filter.FilterParameter.FilterString.Length > 0)
            {
                filterStr = " where " + filter.FilterParameter.FilterString;
            }
            string dataSql = $" INSERT INTO #TEMP EXEC P_ZSLH_DzInventoryWithOrderby";
            string endSql = $" select * ,{seqFld} into {tableName} from #TEMP {filterStr}";
            string sql = createSql + dataSql + endSql;
            DBUtils.ExecuteDynamicObject(this.Context, sql);

            //List<SqlParam> sqlParams = new List<SqlParam>();
            //sqlParams.Add(new SqlParam("@TempName", KDDbType.AnsiString, tableName));
            //sqlParams.Add(new SqlParam("@filterStr", KDDbType.AnsiString, filterStr));
            //DBUtils.ExecuteStoreProcedure(this.Context, "P_ZSLH_DzInventory", sqlParams);
        }
        //protected override string BuilderFromWhereSQL(IRptParams filter)
        //{
        //    return base.BuilderFromWhereSQL(filter);
        //}
        public override List<SummaryField> GetSummaryColumnInfo(IRptParams filter)
        {
            var result = base.GetSummaryColumnInfo(filter);
            result.Add(new SummaryField("FBASEQTY", Enu_SummaryType.SUM));
            return result;
        }
    }
}
